package day16.myreflect02;

import java.lang.reflect.Constructor;

/**
 * @author Xiao
 * @date 2021/8/10
 */
public class ConstructorDemo01 {
    /**
     *  反射获取构造方法
     *  	Constructor<?>[] getConstructors()：返回所有公共构造方法对象的数组
     * 	    Constructor<?>[] getDeclaredConstructors()：返回所有构造方法对象的数组
     *  	Constructor<T> getConstructor(Class<?>... parameterTypes)：返回单个公共构造方法对象
     *  	Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)：返回单个构造方法对象
     */
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
        Class<?> clazz = Class.forName("day16.myreflect02.Student02");
        /**返回所有公共构造方法对象的数组*/
        Constructor<?>[] cons = clazz.getConstructors();
        /**返回所有构造方法对象的数组*/
        Constructor<?>[] declCons = clazz.getDeclaredConstructors();
        /**返回单个公共构造方法对象*/
        //小括号中一定要跟形参的内容保持一致
        Constructor<?> con1 = clazz.getConstructor();
        Constructor<?> con2 = clazz.getConstructor(String.class, Integer.class);
        /**返回单个构造方法对象*/
        Constructor<?> delcCon = clazz.getDeclaredConstructor(String.class);
//        method1(clazz);
//        method2(clazz);
//        method3(clazz);
//        method4(clazz);
    }

    private static void method4(Class<?> clazz) throws NoSuchMethodException {
        Constructor<?> delcCon = clazz.getDeclaredConstructor(String.class);
        System.out.println(delcCon);
    }

    private static void method3(Class<?> clazz) throws NoSuchMethodException {
        //小括号中一定要跟形参的内容保持一致
        Constructor<?> con1 = clazz.getConstructor();
        Constructor<?> con2 = clazz.getConstructor(String.class, Integer.class);
        System.out.println(con1);
        System.out.println(con2);
    }

    private static void method2(Class<?> clazz) {
        Constructor<?>[] declCons = clazz.getDeclaredConstructors();
        for (Constructor<?> declCon : declCons) {
            System.out.println(declCon);
        }
    }

    private static void method1(Class<?> clazz) {
        Constructor<?>[] cons = clazz.getConstructors();
        for (Constructor<?> con : cons) {
            System.out.println(con);
        }
    }
}
